package com.ruoyi.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.edu.api.domain.EduChapter;
import com.ruoyi.edu.api.domain.EduVideo;
import com.ruoyi.edu.api.domain.vo.EduChapterVO;
import com.ruoyi.edu.api.domain.vo.EduVideoVO;
import com.ruoyi.edu.mapper.EduChapterMapper;
import com.ruoyi.edu.mapper.EduVideoMapper;
import com.ruoyi.edu.service.IEduChapterService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程章节Service业务层处理
 *
 * @author isahino
 * @date 2023-09-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {
    @Resource
    private EduChapterMapper eduChapterMapper;
    @Resource
    private EduVideoMapper eduVideoMapper;

    /**
     * 查询课程章节
     *
     * @param id 课程章节主键
     * @return 课程章节
     */
    @Override
    public EduChapter selectEduChapterById(String id) {
        return eduChapterMapper.selectEduChapterById(id);
    }

    /**
     * 查询课程章节列表
     *
     * @param eduChapter 课程章节
     * @return 课程章节
     */
    @Override
    public List<EduChapter> selectEduChapterList(EduChapter eduChapter) {
        return eduChapterMapper.selectEduChapterList(eduChapter);
    }

    /**
     * 新增课程章节
     *
     * @param eduChapter 课程章节
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEduChapter(EduChapter eduChapter) {
        return eduChapterMapper.insert(eduChapter);
    }

    /**
     * 修改课程章节
     *
     * @param eduChapter 课程章节
     * @return 结果
     */
    @Override
    public int updateEduChapter(EduChapter eduChapter) {
        return eduChapterMapper.updateById(eduChapter);
    }

    /**
     * 批量删除课程章节
     *
     * @param ids 需要删除的课程章节主键
     * @return 结果
     */
    @Override
    public int deleteEduChapterByIds(String[] ids) {
        return eduChapterMapper.deleteEduChapterByIds(ids);
    }

    /**
     * 删除课程章节信息
     *
     * @param id 课程章节主键
     * @return 结果
     */
    @Override
    public int deleteEduChapterById(String id) {
        return eduChapterMapper.deleteEduChapterById(id);
    }

    @Override
    public List<EduChapterVO> getChapterVideo(String id) {
        //1.查询所有章节
        LambdaQueryWrapper<EduChapter> chapterWrapper = new LambdaQueryWrapper<>();
        chapterWrapper.eq(EduChapter::getCourseId, id);
        List<EduChapter> chapterList = this.baseMapper.selectList(chapterWrapper);

        //2.查询所有的小节
        LambdaQueryWrapper<EduVideo> videoWrapper = new LambdaQueryWrapper<>();
        videoWrapper.eq(EduVideo::getCourseId, id);
        List<EduVideo> videoList = eduVideoMapper.selectList(videoWrapper);

        //定义最终的数据结果
        List<EduChapterVO> finalChapterList = new ArrayList<>(chapterList.size());

        //3.遍历章节集合,将其封装到List <ChapterVo>
        for (EduChapter chapter : chapterList) {
            EduChapterVO chapterVO = new EduChapterVO();
            BeanUtils.copyProperties(chapter, chapterVO);
            //将其放入结果集
            finalChapterList.add(chapterVO);
            List<EduVideoVO> children = new ArrayList<>();
            //4.遍历小节集合,将其封装到对应的章节中
            for (EduVideo video : videoList) {
                if (video.getChapterId().equals(chapter.getId())) {
                    EduVideoVO videoVO = new EduVideoVO();
                    BeanUtils.copyProperties(video, videoVO);
                    children.add(videoVO);
                }
            }
            chapterVO.setChildren(children);
        }
        return finalChapterList;
    }
}
